package utils;

import LeetCode._01算法入门.day08广度深度优先搜索.Node;
import LeetCode._01算法入门.day10递归回溯.ListNode;

/**
 * @author 挚爱之夕
 * @date 2022-02-27 - 02 - 27 - 10:14
 * @Description utils
 * @Version 1.0
 */
public class MyPrintUtil {
    /**
     * 打印二维数组
     * @param array 二维数组
     */
    public static void printArray(int[][] array){
        for (int[] ts : array) {
            for (int j = 0; j < array[0].length; j++) {
                System.out.print(ts[j] + "\t");
            }
            System.out.println();
        }
    }

    /**
     * 打印完美二叉树（节点具有next属性指向同层的右侧节点）
     * @param root 根节点
     */
    public static void printPerfectTree(Node root){
        if(root == null){
            return;
        }
        if(root.left == null && root.next == null){//最后一个
            System.out.print(root.val);
            return;
        }
        Node head = root;
        System.out.print(head.val);
        while(head.next != null){
            head = head.next;
            System.out.print("->" + head.val);
        }
        System.out.println();
        printPerfectTree(root.left);
    }

    /**
     * 打印单链表
     * @param head 表头
     */
    public static void printSingleLinkedList(ListNode head){
        ListNode p = head;
        while(p != null){
            System.out.print(p.val);
            if(p.next != null){
                System.out.print("->");
            }
            p = p.next;
        }
    }
}
